package framework.temporal;

import external.TableCellListener;
import framework.FRDController;
import framework.FRDModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Lukáš Marek
 */
public class FRDTemporalController extends FRDController implements ActionListener
{
	protected FRDTemporal temporalPanel;
	protected FRDTemporalModel model;
	
	public FRDTemporalController(FRDTemporal panel, FRDModel model)
	{
		this.temporalPanel = panel;
		this.model = (FRDTemporalModel) model;
		TableCellListener tcl = new TableCellListener(this.temporalPanel.jTable, new Action());
		initListeners();
	}

	/**
	 * inicializace listeneru
	 */
	private void initListeners()
	{
		temporalPanel.deleteButton.addActionListener(this);
		temporalPanel.filterButton.addActionListener(this);
	}
	
	/**
	 * zpracovani kliknuti na nektere tlacitko
	 * @param e event
	 */
	@Override
	public void actionPerformed(ActionEvent e)
	{
		JButton button = (JButton) e.getSource();
		switch (button.getActionCommand())
		{
			case "filterButton":
				loadItems();
				break;
			case "deleteButton":
				int[] selectedRows = new int[temporalPanel.jTable.getSelectedRows().length];
				for (int i = 0; i < temporalPanel.jTable.getSelectedRows().length; i++)
				{
					String row = (String) temporalPanel.jTable.getModel().getValueAt(temporalPanel.jTable.getSelectedRows()[i], 0);
					selectedRows[i] = Integer.valueOf(row);
				}
				model.deleteColumns(selectedRows);
				loadItems();
				break;
			default:
				System.err.println(button.getActionCommand());
				break;
		}
	}
	
	@Override
	protected LinkedList<String> setParameters()
	{
		LinkedList<String> ret = new LinkedList<>();
		ret.add(temporalPanel.getFilterName());
		ret.add(temporalPanel.getFilterFrom());
		ret.add(temporalPanel.getFilterTo());
		return ret;
	}
	
	/**
	 * Nacte z modelu data z databaze, data musi by ulozeny v seznamu (LinkedList) polozek typu FRDTemporalDBItem
	 */
	@Override
	public void loadItems()
	{
		LinkedList<FRDTemporalDBItem> DBItems;
		LinkedList<String> params = setParameters();
		if (model != null)
		{
			DBItems = model.getData(params);
			((DefaultTableModel)temporalPanel.jTable.getModel()).setRowCount(0);
			if (DBItems.size() > 0)
			{
				Object Data[][] = new Object[DBItems.size()][model.getColumnNames().size()];
				for (int i = 0; i < DBItems.size(); i++)
				{
					Data[i] = DBItems.get(i).getData().toArray();
				}
				temporalPanel.jTable.setModel(new FRDDefaultTableModel(Data, model.getColumnNames().toArray(), model.getColumnCanEdit()));
			}
		}
	}
	
	/**
	 * trida zpracovavajici akci, ktera je manualni vytvorena knihovnou TableCellListener, zmeni udaj v tabulce a ulozi ho do databaze
	 */
	protected class Action extends AbstractAction
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			TableCellListener tcl = (TableCellListener)e.getSource();
			int id = Integer.parseInt((String)temporalPanel.jTable.getValueAt(tcl.getRow(), 0));
			model.updateColumn(id, tcl.getColumn(), (String)tcl.getNewValue());
		}
	}
	
	/**
	 * Trida pro definovani modelu dat tabulky, pouziva se pro definovani, ktere sloupce pujdou editovat
	 */
	protected class FRDDefaultTableModel extends DefaultTableModel
	{
		boolean[] canEdit;

		public FRDDefaultTableModel(Object[][] Data, Object[] columns, boolean[] canEdit)
		{
			super(Data, columns);
			this.canEdit = canEdit;
		}
		
		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex)
		{
			if (canEdit.length > columnIndex)
				return canEdit[columnIndex];
			else
				return false;
		}
	}
}
